package com.dhcc.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dhcc.entity.ClientUser;
import com.dhcc.entity.SysUserResource;
import com.dhcc.httpApiService.IdGeneratorService;
import com.dhcc.mapper.SysUserResourceMapper;
import com.dhcc.service.ClientUserService;
import com.dhcc.service.InstitutionRoleDepartmentPositionService;
import com.dhcc.service.SysUserResourceService;
import com.dhcc.utils.AuthUtil;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.sql.Date;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

@Service
public class SysUserResourceServiceImpl extends ServiceImpl<SysUserResourceMapper, SysUserResource> implements SysUserResourceService {
    @Resource
    private SysUserResourceMapper sysResourceMapper;
    @Resource
    private InstitutionRoleDepartmentPositionService institutionRoleDepartmentPositionService;
    @Resource
    private IdGeneratorService idGeneratorService;
    @Resource
    private ClientUserService clientUserService;
    @Override
    public SysUserResource getResourceList(String resourceId) {
        SysUserResource sysResource=sysResourceMapper.getResourceList(resourceId);
        return sysResource;
    }

    @Override
    public SysUserResource getResourceById(String resourceId) {
        return sysResourceMapper.getResourceById(resourceId);
    }

    @Override
    public List<SysUserResource> selectResourceList(String institutionId, String institutionRoleId, String departmentId, String positionId) {
        //查询资源编号列表
        List<String> resourceIds= institutionRoleDepartmentPositionService.getResourceIds(institutionId,institutionRoleId,departmentId,positionId);
        List<SysUserResource> sysUserResourceList=new ArrayList<>();
        for (String resourceId : resourceIds) {
            SysUserResource resource = sysResourceMapper.getResourceById(resourceId);
            sysUserResourceList.add(resource);
        }
        return sysUserResourceList;
    }

    @Override
    public Integer addResource(SysUserResource sysUserResource) {
        return sysResourceMapper.addResource(sysUserResource);
    }

    @Override
    public Integer updateResource(SysUserResource sysUserResource) {
        return sysResourceMapper.updateResource(sysUserResource);
    }

    @Override
    public Integer deleteResource(String resourceId) {
        String userId = AuthUtil.getUserId();
        ClientUser user = clientUserService.getUserById(userId);
        // 获取当前时间
        LocalDateTime now = LocalDateTime.now();

        // 定义日期时间格式化器，格式化为 "yyyy-MM-dd HH:mm:ss"
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

        // 格式化当前时间
        String formattedDateTime = now.format(formatter);
        Date date = Date.valueOf(formattedDateTime);
        return sysResourceMapper.deleteResource(user.getName(),date,resourceId);
    }

    @Override
    public List<SysUserResource> getResourceListByParentId(String resourceId) {
        return sysResourceMapper.getResourceListByParentId(resourceId);
    }

    @Override
    public List<SysUserResource> getAllResourceList() {
        return sysResourceMapper.getAllResourceList();
    }
}
